﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Filters;
using TEC.Public.Component.Api;
using TEC.Public.Component.Api.WebApi;
using TongTongMall.Dependecy;
using TongTongMall.MallWeb.Core.Tenant.Services;
using TongTongMall.Threading;
using TongTongMall.UI;

namespace TongTongMall.MallWeb.WebApi.Filter
{
    public class InnerApiAttribute : ApiFilterBaseAttribute
    {
        private readonly IConfigDomainService _IConfigDomainService;

        public InnerApiAttribute()
        {
            _IConfigDomainService = IocManager.Instance.Resolve<IConfigDomainService>();

        }
        public override AopServerConfig GetConfig(string appid)
        {
            return AsyncHelper.RunSync(() =>
            {
                return
                   _IConfigDomainService.GetInnerServerApiConfig(appid);
            });
        }

        public override ApiAopErrMsg GetErrMsg(Exception ex)
        {
            var err = new ApiAopErrMsg();

            if (ex is UserFriendlyException)
            {
                var friendlyex = ex as UserFriendlyException;
                err.Code = friendlyex.Code;
                err.Msg = friendlyex.Message;
            }
            return err;
        }

        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            base.OnActionExecuted(actionExecutedContext);
        }
    }
}